package com.example.qqviewspringboot.controller;

import com.example.qqviewspringboot.data.User;
import com.example.qqviewspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/users")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    // 注册用户
    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> registerUser(@RequestBody User user) {
        Map<String, Object> response = new HashMap<>();
        try {
            userService.registerUser(user);
            response.put("result", true);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            response.put("result", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 用户登录
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> loginUser(@RequestBody User user) {
        Map<String, Object> response = new HashMap<>();

        // 调用服务层方法验证用户身份
        Optional<User> loggedInUser = userService.loginUser(user.getUsername(), user.getPassword());

        if (loggedInUser.isPresent()) {
            // 登录成功，返回用户ID和成功标志
            response.put("result", true);
            response.put("id", loggedInUser.get().getId());
            return ResponseEntity.ok(response);
        } else {
            // 登录失败，返回失败标志和错误信息
            response.put("result", false);
            response.put("message", "Invalid username or password");
            return ResponseEntity.status(401).body(response);
        }
    }


    // 获取单个用户信息
    @GetMapping("/{id}")
    public ResponseEntity<User> getUserInfo(@PathVariable Long id) {
        return userService.getUserInfo(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    // 更新用户信息
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateUserInfo(@PathVariable Long id, @RequestBody Map<String, String> params) {
        Map<String, Object> response = new HashMap<>();
        try {
            String newUsername = params.get("username");
            userService.updateUserInfo(id, newUsername);
            response.put("result", true);
            response.put("message", "用户信息更新成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("result", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取所有用户列表
    @GetMapping("/list")
    public ResponseEntity<List<User>> getAllUsers() {
        List<User> users = userService.getAllUsers();
        return ResponseEntity.ok(users);
    }
}
